package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.FoodType;
import com.hcrazy.manchester.app.pojo.PageModel;

public class FoodTypeDAO extends BaseDAO<FoodType>{

	
	public void addFoodType(FoodType foodType) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_book_food_type(");
		sb.append("typeName,sortNum");
		sb.append(") values (");
		sb.append(":typeName,:sortNum");
		sb.append(")");
		
		update(sb.toString(), foodType);
	}
	
	public void updateFoodType(FoodType foodType) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_book_food_type set ");
		sb.append(" typeName=:typeName,sortNum=:sortNum");
		sb.append(" where typeId = :typeId");
		
		update(sb.toString(), foodType);
	}
	
	public FoodType getFoodTypeById(long typeId) throws Exception{
		String sql = "select * from him_book_food_type where typeId = ?";
		return getJavaBean(sql, FoodType.class, typeId);
	}
	
	public PageModel getPagedFoodTypes(PageModel model,String search) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from him_book_food_type ");
		StringBuilder countSQL = new StringBuilder("select count(*) from him_book_food_type ");
		StringBuilder whereSQL = new StringBuilder(" where typeName like ?");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+search+"%");
		
		whereSQL.append(" order by sortNum");
		
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, FoodType.class);
	}
	
	public void deleteFoodType(long typeId) throws Exception{
		String sql = "delete from him_book_food_type where typeId = ?";
		commonUpdate(sql, typeId);
	}
	
	public List<FoodType> getList() throws Exception{
		String sql = "select * from him_book_food_type";
		return getList(sql,FoodType.class);
	}
}
